package com.zuipin.util;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class StringUtil {
	public static NumberFormat nf = NumberFormat.getInstance();
	
	/**
	 * 填充字符串
	 * 
	 * @author wangcongyi
	 * @param value
	 *            当前值
	 * @param count
	 *            字符串位数
	 * @param frontORback
	 *            填充位置（前/后）
	 * @param fillChar
	 *            填充字符
	 * @return 填充后的字符串
	 */
	public static String fillStr(String value, int count, boolean frontORback, String fillChar) {
		String rtvalue = value;
		if (rtvalue == null) {
			rtvalue = "";
			for (int i = 0; i < count; i++)
				if (frontORback) {
					rtvalue = String.valueOf(rtvalue) + String.valueOf(fillChar);
				} else {
					rtvalue = String.valueOf(fillChar) + String.valueOf(rtvalue);
				}
		} else {
			int len = rtvalue.length();
			if (len > count) {
				rtvalue = rtvalue.substring(0, count);
			} else {
				int a = count - len;
				for (int i = 0; i < a; i++)
					if (frontORback) {
						rtvalue = String.valueOf(rtvalue) + String.valueOf(fillChar);
					} else {
						rtvalue = String.valueOf(fillChar) + String.valueOf(rtvalue);
					}
			}
		}
		return rtvalue;
	}
	
	/**
	 * <p>
	 * Checks if a String is whitespace, empty ("") or null.
	 * </p>
	 * 
	 * <pre>
	 * StringUtils.isBlank(null)      = true
	 * StringUtils.isBlank("")        = true
	 * StringUtils.isBlank(" ")       = true
	 * StringUtils.isBlank("bob")     = false
	 * StringUtils.isBlank("  bob  ") = false
	 * </pre>
	 * 
	 * @param str
	 *            the String to check, may be null
	 * @return <code>true</code> if the String is null, empty or whitespace
	 * @since 2.0
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * <p>
	 * Checks if a String is not empty (""), not null and not whitespace only.
	 * </p>
	 * 
	 * <pre>
	 * StringUtils.isNotBlank(null)      = false
	 * StringUtils.isNotBlank("")        = false
	 * StringUtils.isNotBlank(" ")       = false
	 * StringUtils.isNotBlank("bob")     = true
	 * StringUtils.isNotBlank("  bob  ") = true
	 * </pre>
	 * 
	 * @param str
	 *            the String to check, may be null
	 * @return <code>true</code> if the String is not empty and not null and not
	 *         whitespace
	 * @since 2.0
	 */
	public static boolean isNotBlank(String str) {
		return !StringUtils.isBlank(str);
	}
	
	/**
	 * <p>
	 * Checks if a String is empty ("") or null.
	 * </p>
	 * 
	 * <pre>
	 * StringUtils.isEmpty(null)      = true
	 * StringUtils.isEmpty("")        = true
	 * StringUtils.isEmpty(" ")       = false
	 * StringUtils.isEmpty("bob")     = false
	 * StringUtils.isEmpty("  bob  ") = false
	 * </pre>
	 * <p>
	 * NOTE: This method changed in Lang version 2.0. It no longer trims the
	 * String. That functionality is available in isBlank().
	 * </p>
	 * 
	 * @param str
	 *            the String to check, may be null
	 * @return <code>true</code> if the String is empty or null
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
	
	/**
	 * <p>
	 * Checks if the String contains only unicode digits. A decimal point is not
	 * a unicode digit and returns false.
	 * </p>
	 * <p>
	 * <code>null</code> will return <code>false</code>. An empty String
	 * (length()=0) will return <code>true</code>.
	 * </p>
	 * 
	 * <pre>
	 * StringUtils.isNumeric(null)   = false
	 * StringUtils.isNumeric("")     = true
	 * StringUtils.isNumeric("  ")   = false
	 * StringUtils.isNumeric("123")  = true
	 * StringUtils.isNumeric("12 3") = false
	 * StringUtils.isNumeric("ab2c") = false
	 * StringUtils.isNumeric("12-3") = false
	 * StringUtils.isNumeric("12.3") = false
	 * </pre>
	 * 
	 * @param str
	 *            the String to check, may be null
	 * @return <code>true</code> if only contains digits, and is non-null
	 */
	public static boolean isNumeric(String str) {
		if (str == null) {
			return false;
		}
		int sz = str.length();
		for (int i = 0; i < sz; i++) {
			if (Character.isDigit(str.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}
	
	public static String substring(String str, int start, int end) {
		if (str == null) {
			return null;
		}
		
		// handle negatives
		if (end < 0) {
			end = str.length() + end; // remember end is negative
		}
		if (start < 0) {
			start = str.length() + start; // remember start is negative
		}
		
		// check length next
		if (end > str.length()) {
			end = str.length();
		}
		
		// if start is greater than end, return ""
		if (start > end) {
			return "";
		}
		
		if (start < 0) {
			start = 0;
		}
		if (end < 0) {
			end = 0;
		}
		
		return str.substring(start, end);
	}
	
	/**
	 * 判断两个字符串是否相同
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean isEqual(String str1, String str2) {
		return str1 == null ? str2 == null : str1.equals(str2);
	}
	
	/**
	 * 判断两个字符串是否相同
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean isEqualNull(String str1, String str2) {
		if ("".equals(str1)) {
			str1 = null;
		}
		if ("".equals(str2)) {
			str2 = null;
		}
		return str1 == null ? str2 == null : str2 == null ? str2 == str1 : str1.trim().equals(str2.trim());
	}
	
	/**
	 * 
	 * @author: zengxinchao
	 * @date: 2017年3月29日 下午7:18:22
	 * @param input
	 * @return
	 * @Description: 获取参数名
	 */
	public static String paramName(String input) {
		if (isNotBlank(input)) {
			int index = input.indexOf(".");
			if (index > 0 && !input.endsWith(".")) {
				return input.substring(0, input.indexOf("."));
			} else if (index < 0) {
				return input;
			}
		}
		return "";
	}
	
	public static boolean isNumber(String input) {
		return (isFloat(input) || isDouble(input) || isInteger(input) || isLong(input));
	}
	
	public static boolean isLong(String input) {
		try {
			Long.parseLong(input);
			return true;
		} catch (NumberFormatException exe) {
			return false;
		}
	}
	
	public static boolean isFloat(String input) {
		try {
			float f = Float.parseFloat(input);
			if (Float.isNaN(f) || f == Float.POSITIVE_INFINITY || f == Float.NEGATIVE_INFINITY) {
				return false;
			} else {
				return true;
			}
		} catch (NumberFormatException ex) {
		}
		return false;
	}
	
	public static boolean isInteger(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (NumberFormatException ex) {
			return false;
		}
	}
	
	public static boolean isDouble(String input) {
		try {
			double d = Double.parseDouble(input);
			return !(Double.isNaN(d) || d == Double.POSITIVE_INFINITY || d == Double.NEGATIVE_INFINITY);
		} catch (NumberFormatException ex) {
		}
		return false;
	}
	
	/**
	 * 字符是否存在字符集中
	 *
	 * @Title: isContains 
	 * @author: wuyicheng
	 * @date: 2017年7月18日下午7:28:31 
	 * @param str 字符串
	 * @param collection 字符串集合
	 * @param separator 分隔符
	 * @return
	 * @return: boolean
	 * @throws
	 */
	public static boolean isContains(String str, String collection, String separator) {
		if (StringUtil.isNotBlank(str) && StringUtil.isNotBlank(collection) && StringUtil.isNotBlank(separator)) {
			String[] array = collection.split(separator);
			for (String s : array) {
				if (s.equals(str)) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 字符串转成list  去空
	 * @Title: splitString 
	 * @Description: TODO
	 * @author xujinding
	 * @param @param str
	 * @param @return
	 * @return List<String>
	 * @throws
	 */
	public static List<String> splitString(String str) {
		List<String> list = new ArrayList<>();
		if (!StringUtil.isBlank(str)) {
			String[] array = str.split(",");
			if (array != null && array.length > 0) {
				for (int i = 0; i < array.length; i++) {
					if (!StringUtil.isBlank(array[i])) {
						list.add(array[i]);
					}
				}
			}
		}
		return list;
		
	}
	
	/**
	 * 将驼峰标识转换为下划线
	 * 
	 * @param text
	 * @return camel
	 */
	public static String camelToUnderline(String text, boolean isLower) {
		if (text == null || "".equals(text.trim())) {
			return "";
		}
		StringBuffer result = new StringBuffer(text.length() + 1);
		result.append(text.substring(0, 1));
		for (int i = 1; i < text.length(); i++) {
			if (!Character.isLowerCase(text.charAt(i))) {
				result.append('_');
			}
			result.append(text.substring(i, i + 1));
		}
		if (isLower) {
			return result.toString().toLowerCase();
		}
		return result.toString().toUpperCase();
	}
	
	/**
	 * 将下划线标识转换为驼峰
	 * 
	 * @param text
	 * @return underline
	 */
	public static String underlineToCamel(String text) {
		if (text == null || "".equals(text.trim())) {
			return "";
		}
		int length = text.length();
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < length; i++) {
			char c = text.charAt(i);
			if (c == '_') {
				if (++i < length) {
					result.append(Character.toUpperCase(text.charAt(i)));
				}
			} else {
				result.append(c);
			}
		}
		return result.toString();
	}
	
	/**
	 * 将String list字符串集合用逗号","分割
	 * 
	 * @param list
	 *            集合
	 * @return string
	 */
	public static String splitListByComma(List<String> list) {
		StringBuffer result = new StringBuffer();
		for (String str : list) {
			result.append(",").append(str);
		}
		return result.substring(1, result.length());
	}
	
}
